package cn.yuanqiao.archive.Identify.service.impl;/**
 * @author 赵常飞
 * @date 2023/9/18 18:26
 */

import cn.yuanqiao.archive.Identify.domain.TenantDispose;
import cn.yuanqiao.archive.Identify.mapper.DisposeMapper;
import cn.yuanqiao.archive.Identify.service.IDisposeService;
import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantItemFolder;
import cn.yuanqiao.archive.appManager.service.ITenantCategoryService;
import cn.yuanqiao.archive.danganzhengli.service.IHandOverDaService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *@AUTHOR:赵常飞
 *@date 2023/9/18 18:26
 */
@Service
public class DisposeServiceImpl implements IDisposeService {
    @Autowired
    private DisposeMapper disposeMapper;
    @Autowired
    private IHandOverDaService handOverDaService;
    @Autowired
    private ITenantCategoryService tenantCategoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int fpcz(JSONObject json) {
        List ids=json.getJSONArray("ids");
        String type=json.getString("type");
        Long categoryId=json.getLong("categoryId");
        for (int i = 0; i < ids.size(); i++) {
            TenantDispose dispose=new TenantDispose();
            dispose.setDaId(Long.parseLong(String.valueOf(ids.get(i))));
            dispose.setCztype(type);
            dispose.setCategoryId(categoryId);
            List<Long> idss=new ArrayList<>(1);
            idss.add(Long.parseLong(String.valueOf(ids.get(i))));
            deletedata(idss,type);
            disposeMapper.fpcz(dispose);
        }
        if("yijiao".equals(type)){


            //在鉴定中,项目类型获取到的是卷id,此处需要获取到项目id
            TenantCategory category = tenantCategoryService.selectTenanCategoryByID(categoryId);
            if(3==category.getType()){
                List<Long> xmids=disposeMapper.getxmidsbyjuanids(new TenantItemFolder().getTableName(),ids.toString().replace("[","").replace("]","").replace("\"",""));
                //使用for循环得到数组
                Long[] array = new Long[xmids.size()];
                for(int i = 0; i < xmids.size();i++){
                    array[i] = Long.parseLong(String.valueOf(xmids.get(i)));
                }
                handOverDaService.insertHandOverDa(categoryId,array);
            }else{
                //使用for循环得到数组
                Long[] array = new Long[ids.size()];
                for(int i = 0; i < ids.size();i++){
                    array[i] = Long.parseLong(String.valueOf(ids.get(i)));
                }
                handOverDaService.insertHandOverDa(categoryId,array);
            }
        }
        return 1;
    }

    @Override
    public int getnum(Long daId, String cztype) {
        TenantDispose dispose=new TenantDispose();
        dispose.setDaId(daId);
        dispose.setCztype(cztype);
        return disposeMapper.getnum(dispose);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletedata(List<Long> ids, String cztype) {
        disposeMapper.deletedata(ids,cztype,new TenantDispose().getTableName());
        return 1;
    }
}
